package b;

import javax.swing.plaf.synth.SynthEditorPaneUI;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-04-25
 * Time: 19:28
 */

/**
 *
 */
public class UdpEchoClient {
    private DatagramSocket socket = null;
    private String serveIP;
    private int servePort;
    public UdpEchoClient(String serveIP,int servePort) throws SocketException {
        socket = new DatagramSocket();
        this.serveIP = serveIP;
        this.servePort = servePort;
    }
    public void start() throws IOException {
        System.out.println("客户端启动!");
        while(true){
            System.out.println("请输入要发送的请求:");
            Scanner scanner = new Scanner(System.in);
            String request = scanner.next();
            DatagramPacket requestPakcet = new DatagramPacket(request.getBytes(),0,request.getBytes().length,
                    InetAddress.getByName(serveIP),servePort);
            socket.send(requestPakcet);
            DatagramPacket responsePacket =new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String response =  new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);
        }
    }
    public static void main(String[] args) throws IOException {
        UdpEchoClient udpEchoClient = new UdpEchoClient("127.0.0.1",9090);
        udpEchoClient.start();

    }
}
